<?php

namespace App\Http\Controllers\Admin\Task;

use App\Enums\TaskEnum;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class TaskTypeController extends BaseController {

    /**
     * @api {post} /task/type 任务类型-创建
     * @apiVersion 0.1.0
     * @apiName 任务管理-任务类型-创建
     * @apiGroup 任务管理
     *
     * @apiParam {number} id                        序号 必传
     * @apiParam {string} name                      任务类型名称 必传
     * @apiParam {string} desc                      任务类型描述 必传
     * @apiParam {number} link_status               状态 1开启2关闭 必传
     * @apiParam {number} link_type                 跳转类型 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|numeric|integer',
                'name' => 'required|string|max:255',
                'desc' => 'required|string|max:255',
                'link_status' => 'required|numeric|integer|in:1,2',
                'link_type' => 'required|numeric',
            ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $params = $request->input();

        if (!isset(TaskEnum::TASK_TYPE_NAME_MAP[$params['id']])) {
            throw new BasicException(10001, "类型不合法！");
        }

        if (!isset(TaskEnum::TASK_LINK_NAME_MAP[$params['link_type']])) {
            throw new BasicException(10001, "跳转类型不合法！");
        }

        $exist = app("model_task_type")->where("id", $params['id'])->first();
        if ($exist) {
            throw new BasicException(10001, "已存在相同类型！");
        }

        $res = app('repo_task');
        $params['add_time'] = time();
        $params['update_time'] = time();

        $rows = $res->addType($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /task/type 任务类型-删除
     * @apiVersion 0.1.0
     * @apiName 任务管理-任务类型-删除
     * @apiGroup 任务管理
     *
     * @apiParam {string} ids                       字符串分割的id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function del(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'ids' => 'required|string',
            ]);

        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $idsStr = $request->input("ids");
        $idsArr = explode("," , $idsStr);

        $rows = app('repo_task')->delType($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /task/type 任务类型-修改
     * @apiVersion 0.1.0
     * @apiName 任务管理-任务类型-修改
     * @apiGroup 任务管理
     *
     *
     * @apiParam {int} id
     * @apiParam {string} name                      任务类型名称 必传
     * @apiParam {string} desc                      任务类型描述 必传
     * @apiParam {number} link_status               状态 1开启2关闭 必传
     * @apiParam {number} link_type                 跳转类型 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|integer',
                'name' => 'required|string|max:255',
                'desc' => 'required|string|max:255',
                'link_status' => 'required|numeric|integer|in:1,2',
                'link_type' => 'required|numeric',
            ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $params = $request->input();
        $id = $params['id'];

        if (!isset(TaskEnum::TASK_LINK_NAME_MAP[$params['link_type']])) {
            throw new BasicException(10001, "跳转类型不合法！");
        }

        $params = $request->input();

        $res = app('repo_task');
        $params['update_time'] = time();

        $params = $request->input();

        $rows = $res->saveType(["id" => $id], $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /task/type 任务类型-列表
     * @apiVersion 0.1.0
     * @apiName 任务管理-任务类型-列表
     * @apiGroup 任务管理
     *
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
        "list": [
            {
                "id(序号)": 31,
                "name(任务类型名称)": "每日参与抢糖果次数",
                "desc(任务类型描述)": "每日参与抢糖果次数",
                "link_status(1开启2关闭)": 1,
                "link_type(跳转类型)": 1,
                "link_type_name(跳转类型名称)": "跳转到"
            }
        ],
        "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
        },
        "header": {
            "link_map(跳转类型选择范围)": {
                "1": "跳任意房间",
                "2": "跳个人房间",
             },
            "type_map(序号选择范围)": {
                "1": "新手每日汇总任务",
                "2": "新手礼包任务"
            }
        }
     }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $list = app('repo_task')->getTypeQueryList($request->input());

        return Response::sendList($list, [], [
            "type_map" => TaskEnum::TASK_TYPE_NAME_MAP,
            "link_map" => TaskEnum::TASK_LINK_NAME_MAP,
        ]);
    }


}
